#include "loginwidget.h"

#include "PhoneLoginWidget.h"


LoginWidget::LoginWidget(QWidget *parent)
{
    this->setFixedSize(400, 350);
    this->setWindowTitle("登录");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setAttribute(Qt::WA_DeleteOnClose);  // 如果是在栈上定义的变量, 不能 delete, 会使程序崩溃.
    this->setStyleSheet("QWidget { background-color: white; }"
                        "QLineEdit { padding-left: 5px; font-size: 14px; border-radius: 5px; background-color: rgb(235, 235, 235); }"
                        "QPushButton { border: none; border-radius: 5px;} "
                        "QPushButton:pressed {background-color: rgb(235, 235, 235);}");

    // 1.网格布局
    QGridLayout* layout = new QGridLayout();
    layout->setContentsMargins(50, 0, 50, 0);
    layout->setSpacing(10);
    layout->setAlignment(Qt::AlignVCenter);
    this->setLayout(layout);

    // 2.0标题
    m_tipLabel = new QLabel();
    m_tipLabel->setText("登录");
    m_tipLabel->setAlignment(Qt::AlignCenter);
    m_tipLabel->setFixedHeight(40);
    m_tipLabel->setStyleSheet("QLabel {font-size: 30px;}");

    // 2.1用户名
    m_usernameEdit = new QLineEdit();
    m_usernameEdit->setPlaceholderText("输入用户名");
    m_usernameEdit->setFixedHeight(30);

    // 2.2密码
    m_passwordEdit = new QLineEdit();
    m_passwordEdit->setPlaceholderText("输入密码");
    m_passwordEdit->setFixedHeight(30);
    m_passwordEdit->setEchoMode(QLineEdit::Password);

    // 2.3验证码输入+验证码图案
    m_verifyCodeEdit = new QLineEdit();
    m_verifyCodeEdit->setPlaceholderText("输入验证码");
    m_verifyCodeEdit->setFixedHeight(30);
    m_verifyCodeEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    m_verifyCodeWidget = new VerifyCodeWidget(this);

    // 2.4登录
    m_submitBtn = new QPushButton("登录");
    m_submitBtn->setFixedHeight(30);
    m_submitBtn->setStyleSheet("QPushButton { color: white; background-color: rgb(44, 182, 61); border: none; border-radius: 5px;} "
                               "QPushButton:pressed { background-color: #06AD56; }");
    m_submitBtn->setCursor(Qt::PointingHandCursor);
    connect(m_submitBtn, &QPushButton::clicked, this, &LoginWidget::clickSubmitBtn);

    // 2.5 手机号登录+注册
    m_phoneModeBtn = new QPushButton("手机号登录");
    m_phoneModeBtn->setFixedHeight(30);
    m_phoneModeBtn->setCursor(Qt::PointingHandCursor);
    connect(m_phoneModeBtn, &QPushButton::clicked, this, [=]{
        PhoneLoginWidget* widget = new PhoneLoginWidget();
        widget->show();
        // 关闭当前窗⼝
        this->close();
    });

    m_switchModeBtn = new QPushButton("注册");
    m_switchModeBtn->setFixedHeight(30);
    m_switchModeBtn->setCursor(Qt::PointingHandCursor);
    connect(m_switchModeBtn, &QPushButton::clicked, this, &LoginWidget::switchMode);

    // 3.添加到布局管理器中
    layout->addWidget(m_tipLabel, 0, 0, 1, 5);
    layout->addWidget(m_usernameEdit, 1, 0, 1, 5);
    layout->addWidget(m_passwordEdit, 2, 0, 1, 5);
    layout->addWidget(m_verifyCodeEdit, 3, 0, 1, 3);
    // layout->addWidget(m_verifyCodeBtn, 3, 3, 1, 2);
    layout->addWidget(m_verifyCodeWidget, 3, 3, 1, 2);
    layout->addWidget(m_submitBtn, 4, 0, 1, 5);
    layout->addWidget(m_phoneModeBtn, 5, 0, 1, 1);
    layout->addWidget(m_switchModeBtn, 5, 4, 1, 1);
}

void LoginWidget::clickSubmitBtn()
{
    const QString& user = m_usernameEdit->text().trimmed();
    const QString& pwd = m_passwordEdit->text().trimmed();
    const QString& verifyCode = m_verifyCodeEdit->text().trimmed();
    if(user.isEmpty() || pwd.isEmpty()){
        Toast::showMessage("请输入用户名或密码！");
        return ;
    }
    if(!m_verifyCodeWidget->checkVerifyCode(verifyCode)){
        Toast::showMessage("验证码错误！");
        return ;
    }
    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    if(m_isLoginMode){
        connect(dataCenter, &model::DataCenter::userLoginDone, this, &LoginWidget::userLoginDone, Qt::UniqueConnection);
        dataCenter->userLoginAsync(user, pwd);
        LOG() << "用户名登入";
    }else{
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        connect(dataCenter, &model::DataCenter::userRegisterDone, this, &LoginWidget::userRegisterDone, Qt::UniqueConnection);
        dataCenter->userRegisterAsync(user, pwd);
        LOG() << "用户名注册";
    }
}

void LoginWidget::userLoginDone(bool success, const QString& msg)
{
    if(success == false){
        LOG() << "登入失败：" << msg;
        Toast::showMessage(msg);
        return;
    }

    auto mainWidget = MainWidget::getInstance();
    mainWidget->show();

    this->close();
}

void LoginWidget::userRegisterDone(bool success, const QString& msg)
{
    if(success == false){
        LOG() << "注册失败：" << msg;
        Toast::showMessage(msg);
        return ;
    }

    // 切换模式
    m_isLoginMode = false;
    m_passwordEdit->clear();
    m_verifyCodeEdit->clear();
    m_verifyCodeWidget->refreshVerifyCode();
    switchMode();
}

void LoginWidget::switchMode()
{
    if (m_isLoginMode) {
        // 切换到注册模式
        this->setWindowTitle("注册");
        m_tipLabel->setText("注册");
        m_submitBtn->setText("注册");
        m_switchModeBtn->setText("登录");
        m_isLoginMode = false;
    } else {
        // 切换到登录模式
        this->setWindowTitle("登录");
        m_tipLabel->setText("登录");
        m_submitBtn->setText("登录");
        m_switchModeBtn->setText("注册");
        m_isLoginMode = true;
    }
}
